const express = require('express');
const router = express.Router();
const storyTypeCtrl = require('../controller/storytype');
router.get('/', (req, res, next) => {
	global.navlist = 2;
	storyTypeCtrl.getBookTypeList(data => {
		res.render('list_storytype', {
			list: data
		});
	})
});
router.post('/create', (req, res, next) => {
	storyTypeCtrl.createBookType(req.body, data => {
		if(!data){
			res.json({
				code: -1,
				msg: '添加分类失败',
				data: data
			})
		}else{
			res.json({
				code: 200,
				msg: '添加分类成功',
				data: data
			})
		}
	})
});
router.post('/update', (req, res, next) => {
	storyTypeCtrl.updateBookType(req.body, data => {
		if(!data){
			res.json({
				code: -1,
				msg: '修改分类失败'
			})
		}else{
			res.json({
				code: 200,
				msg: '修改分类成功'
			})
		}
	})
});

router.post('/delete', (req, res, next) => {
	storyTypeCtrl.delBookType(req.body.id, data => {
		console.log(data);
		if(!data){
			res.json({
				code: -1,
				msg: '删除分类失败'
			})
		}else{
			res.json({
				code: 200,
				msg: '删除分类成功'
			})
		}
	})
});

router.post('/sort', (req, res, next) => {
	storyTypeCtrl.sortBookType(req.body, data => {
		res.json({
			code: 200,
			msg: '更新分类排序成功',
			data: data
		})
	})
});

module.exports = router;